package com.wangwen.wechatmp.base.impl;

import com.wangwen.wechatmp.base.BaseService;
import com.wangwen.wechatmp.dao.base.BaseDao;
import com.wangwen.wechatmp.base.BaseService;
import com.wangwen.wechatmp.dao.base.BaseDao;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * 基础service
 * @author  wangwei
 * @param <T>
 * @param <PK>
 */
public abstract class BaseServiceImpl<T,PK extends Serializable> implements BaseService<T,PK> {
    protected abstract BaseDao<T, PK> getDao();

    @Override
    public T findById(PK id){
        return getDao().findById(id).get();
    }
    @Override
    public List<T> findAll(){
        return (List<T>)getDao().findAll();
    }
    @Override
    @Transactional
    public T save(T entity){
        return getDao().save(entity);
    }
    @Transactional
    public Iterable<T> save(Iterable<T> entities){
        return getDao().saveAll(entities);
    }
    @Override
    @Transactional
    public void remove(T entity){
        getDao().delete(entity);
    }
    @Override
    @Transactional
    public void remove(Iterable<T> entities){
        getDao().deleteAll(entities);
    }
    @Override
    @Transactional
    public void remove(PK id){
        getDao().deleteById(id);
    }

    /**
     *
     * @param page
     * @param sepc
     */
    @Override
    public Page<T> findPage(Pageable page, Specification<T> sepc){
        return getDao().findAll(sepc, page);
    }
}